<style type="text/css">
	#tpl-file-tool-id{
		padding: 20px
	}

	#tpl-file-tool-id .icon{height: 38px;line-height: 38px;text-align: center;}
	#tpl-file-tool-id .icon i{font-size: 2rem;font-weight: 800;color: blue;margin-left: 1rem;margin-right: 1rem;cursor: pointer;color: #717171}
	.tpl-file-tool-id-collect{
		color: #ffb800 !important;
	}
</style>
<script type="text/html" id='tpl-file_tool'>
<div id='tpl-file-tool-id'>
	<div class="icon">
		{{# if(d.collect==1){ }}
		<i data-type='2' data-collect_status=1 class="layui-icon layui-icon-star-fill tpl-file-tool-id-collect" title="取消收藏"></i>
		{{# }else{ }}
		<i data-type='2' data-collect_status=0 class="layui-icon layui-icon-star" title="收藏"></i>
		{{# } }}
		{{# if(d.type==1){ }}
		<i data-type='3' class="layui-icon layui-icon-download-circle" title="下载"></i>
		{{# } }}
		<!-- <i data-type='5' class="layui-icon layui-icon-log" title="历史版本"></i> -->
		<i data-type='4' class="layui-icon layui-icon-share" title="分享"></i>
	</div>
</div>
</script>
<script type="text/javascript">
	app.defTpl('file_tool',function(arg){
		layui.use(['layer','laytpl'],function(){
			var laytpl = layui.laytpl,$=layui.$;
			app.ajaxPost('/api/file/getInfoByFileId',{fileId:arg.file_id,collect:1},function(data){
				var d=data.data;
				laytpl($('#tpl-file_tool').html()).render(d, function(html){
				  	$(arg.elem).html(html)
				  	$('#tpl-file-tool-id .icon i').click(function(){
						console.log('图标被点击');
						if($(this).data('type')==2){
							// 收藏/取消
							console.log($(this).data('collect_status'))
							if($(this).data('collect_status')==1){
								// 取消
								delCollect($(this))
							}else{
								// 收藏
								addCollect($(this))
							}
							
						}else if($(this).data('type')==3){
							// 下载
							window.open('/source/download?file_id='+d.id)
						}else if($(this).data('type')==4){
							// 分享
							layer.confirm('确定分享？', {
								btn: ['生成链接', '取消'] //按钮
							}, function (index) {
								app.runTpl('file_share', { file_id: d.id });
								layer.close(index)
							})
						}
					})
				});
			})

			function addCollect(o){
				console.log('添加收藏')
				app.ajaxPost('/api/collect/add',{fileId:arg.file_id},function(){
					o.removeClass('layui-icon-star');
					o.addClass('layui-icon-star-fill');
					o.addClass('tpl-file-tool-id-collect');

					o.data('collect_status',1)
					o.attr('title','取消收藏')
				});
			}

			function delCollect(o){
				console.log('删除收藏')
				app.ajaxPost('/api/collect/delete', { fileId: arg.file_id },function(){
					o.removeClass('layui-icon-star-fill');
					o.removeClass('tpl-file-tool-id-collect');
					o.addClass('layui-icon-star');
					o.data('collect_status',0)
					o.attr('title','收藏')
				});
			}











		})
	})
	
</script>